//
//  ShareMyTableViewCell.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/4.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol ShareMyTableViewCellDelegate:NSObjectProtocol {
    func shareActionDele(cell:UITableViewCell)
    func freeActionDele(cell:UITableViewCell)
}

class ShareMyTableViewCell: UITableViewCell {
    var indextLabel:UILabel = UILabel()
    var leftImage:UIImageView = UIImageView()
    var nameLabel:UILabel = UILabel()
    var titleLabel:UILabel = UILabel()
    var priceLable:UILabel = UILabel()
    var leftBtn:UIButton = UIButton()
    var rightBtn:UIButton = UIButton()
    var lineView:UIView = UIView()
    
    var delegate:ShareMyTableViewCellDelegate?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
    }
    
    
    func setUI() {
        indextLabel.font = Font_Medium(20)
        indextLabel.textAlignment = .center
        indextLabel.textColor = RGB_Same(0)
        contentView.addSubview(indextLabel)
        
        leftImage.layer.cornerRadius = 3
        leftImage.layer.masksToBounds = true
        contentView.addSubview(leftImage)
        
        nameLabel.font = Font_Medium(17)
        nameLabel.textAlignment = .left
        nameLabel.textColor = RGB_Same(0)
        contentView.addSubview(nameLabel)
        
        titleLabel.font = Font_Medium(13)
        titleLabel.textAlignment = .left
        titleLabel.textColor = RGB_Same(53)
        contentView.addSubview(titleLabel)
        
        priceLable.font = Font_Medium(14)
        priceLable.textColor = RGB(230, 67, 64)
        priceLable.textAlignment = .right
        contentView.addSubview(priceLable)
        
        leftBtn = UIButton(type: UIButton.ButtonType.custom)
        leftBtn.setTitle("分享详情", for: .normal)
        leftBtn.setTitleColor(.white, for: .normal)
        leftBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        leftBtn.layer.cornerRadius = 4
        leftBtn.addTarget(self, action: #selector(shareBtnAction), for: .touchUpInside)
        leftBtn.backgroundColor = RGB(66, 166, 59)
        contentView.addSubview(leftBtn)
        
        rightBtn = UIButton(type: UIButton.ButtonType.custom)
        rightBtn.setTitle("免费听", for: .normal)
        rightBtn.setTitleColor(.white, for: .normal)
        rightBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        rightBtn.layer.cornerRadius = 4
        rightBtn.addTarget(self, action: #selector(freeBtnAction), for: .touchUpInside)
        rightBtn.backgroundColor = RGB(255, 151, 29)
        contentView.addSubview(rightBtn)
        
        lineView.backgroundColor = RGB_Same(240, 1)
        contentView.addSubview(lineView)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    @objc func shareBtnAction() {
        delegate?.shareActionDele(cell: self)
    }
    
    @objc func freeBtnAction() {
        delegate?.freeActionDele(cell: self)
    }
    
    func setFree() {
        rightBtn.setTitle("免费听", for: .normal)
        rightBtn.backgroundColor = RGB(255, 151, 29)
    }
    
    func setNext() {
        rightBtn.setTitle("继续分享", for: .normal)
        rightBtn.backgroundColor = RGB(230, 67, 64)
    }
    
    func setFail() {
        rightBtn.setTitle("失败", for: .normal)
        rightBtn.backgroundColor = RGB_Same(178)
    }
    
    func setDate(shareTime:Int, survivalTime:Int) {
        let reTime = shareTime + survivalTime
        let nowTime = Int(Date().timeIntervalSince1970)
        
        if reTime > nowTime {
            let tt = reTime - nowTime
            self.timingAction(tt: tt)
            
        } else {
            self.titleLabel.text = "已结束"
        }
        
    }
    
    func timingAction(tt:Int) {
        
        var time = tt
        let codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
        codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000))
        codeTimer.setEventHandler {
            
            time = time - 1

            if time < 0 {
                codeTimer.cancel()
                DispatchQueue.main.async {
                    self.titleLabel.text = "已结束"
                }
                return
            }
            
            DispatchQueue.main.async {
                self.titleLabel.text = String.returnTimeTofammater(times: TimeInterval(time)) + "后结束"
            }
            
        }
        
        if #available(iOS 10.0, *) {
            codeTimer.activate()
        } else {
            self.titleLabel.text = "已结束"
        }


    }

    override func layoutSubviews() {
        indextLabel.frame = FRAME_CGFloat(0, 38, 32, 28)
        leftImage.frame = FRAME_CGFloat(32, 10, 84, 84)
        nameLabel.frame = FRAME_CGFloat(126, 10, SCREEN_WIDTH - 200, 24)
        titleLabel.frame = FRAME_CGFloat(126, 37.5, SCREEN_WIDTH - 200, 20)
        priceLable.frame = FRAME_CGFloat(SCREEN_WIDTH - 100, 13, 83.5, 24)
        leftBtn.frame = FRAME_CGFloat(SCREEN_WIDTH - 178, 66, 79, 28)
        rightBtn.frame = FRAME_CGFloat(SCREEN_WIDTH - 89, 66, 79, 28)
        lineView.frame = FRAME_CGFloat(0, 103, SCREEN_WIDTH, 1)
    }

}
